<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>订单管理系统</title>
    <link rel="icon" href="/static/img/favicon.ico">
    <link rel="stylesheet" href="/static/plugins/bootstrap/css/bootstrap.css">
    <style>
        .total {
            position: absolute;
            left: 0;
            right: 0;
            top: 0;
            bottom: 0;
            background-color: #cecccc;
        }

        .box {
            width: 400px;
            border: solid 2px #dddd;
            margin: 0 auto;
            margin-top: 100px;
            padding: 0 20px 20px 20px;
        {#添加阴影#} box-shadow: 10px 10px 10px rgb(0 0 0 / 5%);
        }

        .error_message {
            color: red
        }
    </style>
</head>
<body>
<div class="total">
    <div class="box">
        <form action="" method="post" id="smsForm">
            <h2 class="h2 text-center">验证码登录</h2>

            {% for foo in obj %}
                {% if foo.name == 'code' %}
                    <div class="form-group">
                        {{ foo.label }}
                        <div class="row">
                            <div class="col-md-7">
                                {{ foo }}
                                <span class="error_message">{{ foo.errors.0 }}</span>
                            </div>
                            <div class="col-md-5">
                                <button type="button" class="btn btn-info" id="getCode">点击获取验证码</button>
                            </div>
                        </div>
                    </div>
                {% else %}
                    <div class="form-group">
                        {{ foo.label_tag }}
                        {{ foo }}
                        <span class="error_message">{{ foo.errors.0 }}</span>
                    </div>
                {% endif %}
            {% endfor %}

            <div>
                <input type="button" value="登录" class="btn btn-success" id="smsLogin">
                <a href="/login.html" class="btn btn-primary" style="float: right;">账号登录</a>
            </div>
        </form>
    </div>
    <div style="width:407px;margin:0 auto; padding:20px 0;position:absolute;bottom:0;margin-left: 450px">
        <a href="https://beian.miit.gov.cn/" target="_blank">
            <img src="/static/img/备案图标.png" style="float:left;">
            <p style="float:left;margin: 0px 0px 0px 5px;color:#939393;">皖ICP备2023006947号</p>
        </a>
        <p style="float:left;margin: 0px 0px 0px 5px;color:#939393;">|&nbsp&nbsp</p>
        <a target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=34018102340737"
           style="display:inline-block;text-decoration:none;height:20px;line-height:20px;">
            <img src="/static/img/备案图标.png" style="float:left;"/>
            <p style="float:left;height:20px;margin: 0px 0px 0px 5px; color:#939393;">皖公网安备 34018102340737号</p>
        </a>
    </div>

</div>

<script src="/static/js/jquery.min.js"></script>
<script src="/static/js/jquery.cookie.js"></script>
<script>
    $(function () {
        // 设置全局请求头
        csrfSendSmsEvent()

        // 获取手机号. 发送ajax请求. 并执行跑秒
        getSmsEvent()

        // 验证码登录
        smsLoginEvent();
    })

    function csrfSafeMethod(method) {
        return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method))
    }

    function csrfSendSmsEvent() {
        $.ajaxSetup({
            beforeSend: function (xhr, setting) {
                if (!csrfSafeMethod(setting.type)) {
                    xhr.setRequestHeader('X-CSRFTOKEN', $.cookie('csrftoken'))
                }
            }
        })
    }

    function getSmsEvent() {
        $('#getCode').click(function () {
            $('.error_message').empty()

            $.ajax({
                url: '/sms_send.html',
                type: 'post',
                dataType: 'json',
                data: {'role': $('#id_role').val(), 'mobile': $('#id_mobile').val()},
                success: function (data) {
                    if (data.status) {
                        // 2. 跑秒
                        runSecondEvent()
                    } else {
                        $.each(data.detail, function (k, v) {
                            $('#id_' + k).next().text(v[0])  // 循环每一个错误信息. 给他们下面标签添加错误信息
                        })
                    }
                }
            })
        })
    }

    function runSecondEvent() {
        var btn = $('#getCode')
        // 设置属性为不可点击
        btn.attr("disabled", 'disabled');

        // 开始跑秒
        var time = 60
        var obj = setInterval(function () {
            // 设置内容
            btn.text(time + '秒后重新发送');
            if (time === 0) {
                // 清空定时器
                clearInterval(obj)
                btn.text('点击获取验证码')
                btn.attr('disabled', false);
            }
            time -= 1;
        }, 1000)
    }

    function smsLoginEvent() {
        $('#smsLogin').click(function () {
            // 清空错误信息
            $('.error_message').empty()

            $.ajax({
                url: '/sms/login.html',
                type: 'post',
                data: $('#smsForm').serialize(),
                dataType: 'json',
                success: function (data) {
                    if (data.status) {
                        location.href = '/home.html'
                    } else {
                        // 显示错误信息
                        $.each(data.detail, function (k, v) {
                            $('#id_' + k).next().text(v[0])
                        })
                    }

                }
            })

        })
    }
</script>

</body>
</html>